using System.Threading;
using System.Threading.Tasks;
using MediatR;
using B.S.BasicData.EroorCode;
using B.S.BasicData.Domain;
using B.S.BasicData.Infrature;

public class BreedingEnterpriseInfoUpdateHandler : IRequestHandler<BreedingEnterpriseInfoUpdateRequest, ApiResult<BreedingEnterpriseInfo>>
{
    private readonly IBaseRepository<BreedingEnterpriseInfo> _repository;
    
    public BreedingEnterpriseInfoUpdateHandler(IBaseRepository<BreedingEnterpriseInfo> repository)
    {
        _repository = repository;
    }

    public async Task<ApiResult<BreedingEnterpriseInfo>> Handle(BreedingEnterpriseInfoUpdateRequest request, CancellationToken cancellationToken)
    {
        var entity = await _repository.GetByIdAsync(request.Id);
        if (entity == null)
            return ApiResult<BreedingEnterpriseInfo>.Fail("未找到要修改的养殖企业信息");

        entity.FarmName = request.FarmName;
        entity.Manager = request.Manager;
        entity.StockCount = request.StockCount;
        entity.BatchNo = request.BatchNo;

        try
        {
            await _repository.UpdateAsync(entity);
            return ApiResult<BreedingEnterpriseInfo>.Success(entity, "养殖企业信息修改成功");
        }
        catch (System.Exception ex)
        {
            return ApiResult<BreedingEnterpriseInfo>.Fail($"修改失败: {ex.Message}");
        }
    }
} 